package com.android.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SmsReceiver extends BroadcastReceiver {

	private static final String TAG = "SMSReceiver"; 
	static final String ACTION = "android.provider.Telephony.SMS_RECEIVED"; 
	
	
	public void onReceive(Context context, Intent intent) {
		// ---get the SMS message passed in---
		Log.e(TAG, "++ onReceive ++");
		Bundle bundle = intent.getExtras();
		SmsMessage[] msgs = null;
		String contenido = "CONTENIDO";

		if (bundle != null) {
//			Log.e(TAG, "++ retrieve the SMS ++");
//			// ---retrieve the SMS message received---
//			Object[] pdus = (Object[]) bundle.get("pdus");
//			msgs = new SmsMessage[pdus.length];
//			Log.e(TAG, "++ montar SMS ++");				
//			for (int i = 0; i < msgs.length; i++) {
//				msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
//				str += "SMS from " + msgs[i].getOriginatingAddress();
//				str += " :";
//				str += msgs[i].getMessageBody().toString();
//				str += "\n";
//			}
//			Log.e(TAG, "str = " + str);
			// Descompongo el string por espacios y si encuentra la palabra que buscamos muestra por 
            // por pantalla "CONTENIDO"
			Log.e(TAG, "++ Descompongo SMS ++");
//            String[] comparador = str.split(" ");
           
			Object[] pdus = (Object[]) bundle.get("pdus");
			SmsMessage msgss = SmsMessage.createFromPdu((byte[]) pdus[0]);
			
//            for (int j = 0; j < comparador.length; j++) {

            	if (msgss.getMessageBody().toString().equals("arranca"))
            	{
    				Log.e(TAG, "++ display SMS ++");
            		// ---display the new SMS message---
//    				Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
//    				context.startActivity(new Intent(context, Receiver.class));
    				Toast.makeText(context, " LANZANDO ACTIVIDAD BOTONES", Toast.LENGTH_SHORT).show();
    				Intent smsActivityIntent = new Intent(context, com.android.activity.Receiver.class);
    				smsActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//    				smsActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    				smsActivityIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    		        context.startActivity(smsActivityIntent);		
    		        
    				
//    	            ReceiverSMS.this.finish();
            	}
            	
//            }

		}
	}

}